package br.com.todirango;

import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import br.com.todirando.entity.Empresa;
import br.com.todirando.entity.Genero;
import br.com.todirango.util.Banco;
import br.com.todirango.util.GerenciadorEmpresas;
public class EmpresasActivity extends Activity {
	private ProgressDialog pgd;
	private Banco banco;
	private Genero genero;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_empresas);
	}  
	
	@Override
	protected void onResume() {
		if(getIntent().getSerializableExtra("genero") != null)
			this.genero = (Genero) getIntent().getSerializableExtra("genero");
		
		pgd = ProgressDialog.show(EmpresasActivity.this,"Carregando", "Empresas...", true);
		
		ListView listaEmpresas = (ListView) findViewById(R.id.listEmpresas);
		this.banco = new Banco(getApplicationContext());
		
		String pGenero = String.valueOf(genero.getCodigo());
			
		listaEmpresas.setAdapter(new ArrayAdapter<Empresa>(this,android.R.layout.simple_list_item_1, this.banco.getEmpresa(pGenero)));
		listaEmpresas.setClickable(true);
		listaEmpresas.setCacheColorHint(Color.TRANSPARENT);
		
		listaEmpresas.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int position,
					long id) {
				Intent intent = new Intent(EmpresasActivity.this, DetalhesEmpresaActivity.class);
				intent.putExtra("empresa", (Empresa)adapter.getAdapter().getItem(position));
				startActivity(intent);
			}
		});
			
		pgd.hide();

	super.onResume();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.empresas, menu);
		return true;
	}

}
